package form.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import form.bean.ForgotPwdForm;

/**
 * Classe de validation du bean ForgetPwdForm
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class ForgotPwdFormValidator implements Validator {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean supports(Class clazz) {
		return ForgotPwdForm.class.isAssignableFrom(clazz);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#validate(java.lang.Object,
	 * org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object obj, Errors e) {

		ForgotPwdForm c = (ForgotPwdForm) obj;

		ValidationUtils.rejectIfEmptyOrWhitespace(e, "login", null,
				"login vide");
		if (!e.hasFieldErrors("login")) {

			if (!FieldValidatorUtil.isGoodLength(c.getLogin(), 5, 15)) {
				e.rejectValue("login", null,
						"le login doit contenir entre 5 et 15 caractères");
			}

			if (!FieldValidatorUtil.isAlphaNumString(c.getLogin())) {
				e.rejectValue("login", null,
						"le login contient des caractères interdits");
			}

		}
	}
}
